var dbConfig = require('../utils/dbconfig');

// 获取菜单
getMenuById = (req,res)=>{
    console.log('获取菜单接口 - - -  接到请求');
    //获取请求参数
    // let {userid} = req.query;
    // console.log(userid)
    let userid = 'huang'
    var sql = "select t2.menu_name,t2.menu_id,t2.menu_name,t2.target_path,t2.parent_menu from user_menu_rel t1 left join menu_info t2 on t1.menu_id = t2.menu_id where t1.user_id=? order by t2.menu_level ,t2.menu_index ";
    var sqlArr = [userid];
    var callBack = (err,data) => {
        if(err){
            console.log('链接出错了~')
        }else{
            var rJson = [];
            var rarr=[];
            for(var i=0;i<data.length;i++){
                if(data[i].parent_menu !== null){
                    var getIndex = rarr.findIndex(function (item) {
                        return item.menu_id == data[i].parent_menu;
                    });
                    var _o = rarr[getIndex];
                    if(null == _o.children){
                        var _a = [];
                        _a.push(data[i]);
                        _o.children = _a;
                        rarr[getIndex] = _o;
                    }else{
                        var _a = _o.children;
                        _a.push(data[i]);
                        _o.children = _a;
                        rarr[getIndex] = _o;
                    }
                }else{
                    rarr.push(data[i]);
                }
            }


            // for(var i=0;i<data.length;i++){
            //     var arr = [];
            //     for(var j=0;j<data.length;j++){
            //         if(data[i].id == data[j].parent_menu){
            //             data[i].children = arr;
            //             arr.push(data[j]);
            //         }
            //     }
            // }
            // for(var i=0;i<data.length;i++){
            //     if(data[i].parent_menu == 0){
            //         rJson.push(data[i]);
            //     }
            // }
            // console.log(rJson)


            res.send({
                'code':200,
                'list':rarr
            })
        }
    }

    dbConfig.sqlConnect(sql,sqlArr,callBack);
}


module.exports = {
    getMenuById
}
